//
//  ViewController.m
//  05-UIScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
// 宏定义 命名
// 宏就是系统编译是, 代码的替换(预编译指令)
// 1 全大写
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
// 2 小写的k + 驼峰
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenBounds [UIScreen mainScreen].bounds
@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    
    // 1 contentOffset 默认CGPointZero
    NSLog(@"offset改变前scrollViewFrame:%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变前scrollViewFrame:%@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"offset改变后scrollViewFrame:%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变后scrollViewFrame:%@", NSStringFromCGRect(scrollView.bounds));
    
    // 2.默认CGSizeZero 设置可以滚动的范围 偏移量, 滚动的位置
    scrollView.contentSize = CGSizeMake(SCREENWIDTH * 5, 1000);
    // 不设置 contentOffset contentSize 也可以改变
    // 问题:改变contentOffset 什么变了
    // 改变Offset, 实质上是对scrollView的Bounds 相当于ScrollView的Bounds有了变化
    NSLog(@"offset改变前scrollViewFrame:%@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset改变后scrollViewFrame:%@", NSStringFromCGRect(scrollView.bounds));
    // 3. 添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    // 4.锁定垂直或水平滚动 (45°角, 锁会失效)
    scrollView.directionalLockEnabled = YES;
    // 5.回弹 bounces, 边缘弹动动画关闭NO
     scrollView.bounces = YES;
    // 6.在没有垂直方向的滚动范围时,保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    // 7.在没有垂直方向的滚动范围时,保证水平方向的回弹效果存在
    scrollView.alwaysBounceHorizontal = YES;
    // 8.按页滚动
    scrollView.pagingEnabled = YES;
    // 9.控制ScrollView的滚动视图能否滚动(默认YES)
    scrollView.scrollEnabled = YES;
    // 10.是否显示垂直或水平的滚动指示器.默认YES
    scrollView.showsHorizontalScrollIndicator = YES;
    scrollView.showsVerticalScrollIndicator = YES;
    // 11.滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    // 12.indicatorStyle滚动指示器的颜色(深色, 浅色, 默认)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    // 13.改变偏移量 动画
    // [scrollView setContentOffset:CGPointZero animated:YES]
    // tracking用户是否开始拖动
    // scrollView.tracking
    // dragging 用户是否正在拖动
    // scrollView.dragging
    // decelerating 是否减速中
    // scrollView.decelerating
    // 14. 滚动视图 双击状态栏手势是否存在 默认YES
    // scrollView.scrollsToTop = NO;
    // 15 协议代理
    scrollView.delegate = self;
    // 16.键盘消失模式
    // UIScrollViewKeyboardDismissModeOnDrag 当拖拽开始的时候键盘消失
    // UIScrollViewKeyboardDismissModeNone 无
    // UIScrollViewKeyboardDismissModeInteractive 使劲划键盘消失
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    // 17.缩放系数
    // 如果想进行视图缩放, 就必须实现 viewForZoomingInScrollView方法
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
    [self.view addSubview:scrollView];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, SCREENWIDTH, 200)];
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:textField];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, 10, SCREENWIDTH - 20, 200);
    btn.backgroundColor = [UIColor cyanColor];
    [scrollView addSubview:btn];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  }
// 与其想要绑定的子视图绑定 ScrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void) clickBtn:(UIButton *)sender {
  //UIScrollView * scrollView =
}
// ScrollView执行顺序
// 滚动视图滚动就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // tracking用户touch触摸
    if (scrollView.tracking) {
        NSLog(@"正在拖动滚动");
    }
    // dragging 用户开始滑动
    if (scrollView.dragging) {
        NSLog(@"");
    }
    // decelerating用户触摸结束
    if (scrollView.decelerating) {
        NSLog(@"");
    }
}
// 将要开始拖拽 (拖拽之前)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
   // NSLog(@"开始拖拽");
}
// 结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    // NSLog(@"结束拖拽");
}
// 将要开始进行减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
   // NSLog(@"开始减速");
}
// 已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // NSLog(@"结束减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
